package cn.zysheep.beanutils;


import org.apache.commons.beanutils.BeanUtils;
import org.junit.jupiter.api.Test;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @version v1.0
 * @Author: lyw
 * @Date: 2021/8/9
 */
public class BeanutilsTest {


    /**
     * 赋值旧对象的所有属性到新对象中.即使旧对象的属性关联了其它对象，也会被一起克隆，类似深度拷贝的原理
     */
    @Test
    public  void testCloneBean() {
        ArrayList<Student> students = new ArrayList<>(10);
        Student student = new Student("张三", "男", 12);
        students.add(student);

        Teacher teacher = new Teacher();
        teacher.setName("zysheep");
        teacher.setAge(20);
        teacher.setSex("女");
        teacher.setStudents(students);

        try {
            Object o = BeanUtils.cloneBean(teacher);
            //Teacher(name=zysheep, sex=女, age=20, students=[Student(name=张三, sex=男, age=12)], map=null)
            System.out.println(o.toString());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }


    /**
     * copyProperties(final Object dest, final Object orig)
     * 对于属性名称相同的所有情况(即使类型不一致)，将属性值从源 orig 复制到 dest.
     */
    @Test
    public void testCopyProperties() throws InvocationTargetException, IllegalAccessException {
        Teacher teacher = new Teacher();
        teacher.setName("zysheep");
        teacher.setAge(20);
        teacher.setSex("女");

        TeacherVo teacherVo = new TeacherVo();
        BeanUtils.copyProperties(teacherVo,teacher);

        System.out.println(teacherVo);
    }

    /**
     * Map<String, String> describe(final Object bean)
     * 将 bean 对象的属性提取到 map 中。bean 对象中只有提供了 getXxx 方法的属性才会提取，否则不提取。
     */
    @Test
    public void testDescribe() {
        try {
            Teacher teacher = new Teacher("老吴","女",12,null,null);

            Map<Object, Object> dataMap = new HashMap<>();
            dataMap.put("code", 201);
            dataMap.put("message", "success");

            teacher.setMap(dataMap);
            Map<String, String> describe = BeanUtils.describe(teacher);
            //{sex=女, name=老吴, students=null, map={code=201, message=success}, age=12}
            System.out.println(describe);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * populate(final Object bean, final Map<String, ? extends Object> properties)
     * 将 properties map 的属性值赋给 bean 中同名的属性
     */
    @Test
    public void testPopulate() {
        try {
            Map<String, Object> deptMap = new HashMap<>();
            deptMap.put("name", "李老师");
            deptMap.put("sex", "女");
            deptMap.put("age",16);

            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("code", 202);
            dataMap.put("message", "success");

            deptMap.put("map", dataMap);

            Teacher teacher = new Teacher();

            BeanUtils.populate(teacher, deptMap);
            //Teacher(name=李老师, sex=女, age=16, students=null, map={code=202, message=success})
            System.out.println(teacher);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
